package tankgame01;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Scanner;

public class TankGame extends JFrame {
    private MyPanel mp = null;

    public static void main(String[] args) {
        new TankGame("坦克大战");
    }

    public TankGame(String title) {
        Scanner scanner = new Scanner(System.in);
        File file = new File(Recorder.getRecordPath());
        int choice = 0;
        if (file.exists()) {
            System.out.println("请选择：继续上一局游戏（1） 新游戏（2）");
            choice = scanner.nextInt();
        } else {
            System.out.println("当前没有存档，是否要开启新游戏（1）");
            int newGame = scanner.nextInt();
            if (newGame == 1) {
                choice = 2;
            } else {
                System.out.println("您的输入有误");
            }
        }
        this.setTitle(title);
        mp = new MyPanel(choice);
        this.add(mp);
        this.setSize(1350, 795);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.addKeyListener(mp);
        new Thread(mp).start();
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (mp.winOrLose == 1 || mp.winOrLose == 2) {
                    Recorder.removeRecord();
                } else {
                    Recorder.exitRecord();
                }
                System.exit(0);
            }
        });
    }
}
